package com.micangcaihang.micang.mvp.task.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.micangcaihang.common.base.fragment.BaseFragment;
import com.micangcaihang.common.utils.Properties;
import com.micangcaihang.common.utils.ToastUtils;
import com.micangcaihang.micang.R;
import com.micangcaihang.micang.databinding.FragmentTaskCostDetailFBinding;
import com.micangcaihang.micang.entity.FTaskCostDetailEntity;
import com.micangcaihang.micang.entity.PickerEntity;
import com.micangcaihang.micang.mvp.task.contract.FTaskCostDetailContract;
import com.micangcaihang.micang.mvp.task.presenter.FTaskCostDetailPresenter;
import com.micangcaihang.micang.view.PickerViewHelper;

import java.util.List;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/24
 *     update : 2017/07/24
 *     desc   : 风控 费用明细
 *     version: 1.0
 * </pre>
 */

public class FTaskCostDetailFragment extends BaseFragment<FTaskCostDetailPresenter,
        FragmentTaskCostDetailFBinding> implements FTaskCostDetailContract.View {
    private String id;
    private FTaskCostDetailEntity mRequest;
    private FTaskCostDetailEntity mCostDetailEntity;
    private int pickerType;
    private boolean isNetSuccess;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        id = getArguments().getString("id");
    }

    public static FTaskCostDetailFragment newInstance(String id) {
        Bundle args = new Bundle();
        FTaskCostDetailFragment fragment = new FTaskCostDetailFragment();
        args.putString("id", id);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * 获取资源文件布局
     *
     * @return 资源布局文件layout
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_task_cost_detail_f;
    }

    @Override
    protected void initData() {
        super.initData();
        mRequest = new FTaskCostDetailEntity();
        mCostDetailEntity = new FTaskCostDetailEntity();
        mRequest.setId(id);
        mCostDetailEntity.setId(id);
    }

    @Override
    public void initPresenter() {
        super.initPresenter();
        mPresenter.setId(id);
    }

    /**
     * 初始化按钮监听
     */
    @Override
    protected void initListener() {
        mViewBinding.setClickHandler(new ClickHandler());
    }

    /**
     * 返回下拉框信息
     *
     * @param list
     * @param type
     */
    @Override
    public void returnPickerViewData(List<PickerEntity> list, String type) {
        switch (pickerType) {
            case 1:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getService_pay_condition(), list,
                        pickerEntity1 -> {
                            mRequest.setService_pay_condition(pickerEntity1.getSign_id());
                            mViewBinding.tvServicePayCondition.setText(pickerEntity1.getTitle());

                        });
                break;
            case 2:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getService_pay_type(), list,
                        pickerEntity1 -> {
                            mRequest.setService_pay_type(pickerEntity1.getSign_id());
                            mViewBinding.tvServicePayType.setText(pickerEntity1.getTitle());
                            if ("2".equals(mRequest.getService_pay_type())) {//一次性
                                mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
                                mViewBinding.llyPayMoney.setVisibility(View.GONE);
                            } else if ("3".equals(mRequest.getService_pay_type())) {//按日
                                mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
                                mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
                            } else if ("4".equals(mRequest.getService_pay_type())) {//分期
                                mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
                                mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
                            } else {
                                mViewBinding.llyPayDate.setVisibility(View.GONE);
                                mViewBinding.llyPayMoney.setVisibility(View.GONE);
                            }
                        });
                break;
            case 3:
                PickerViewHelper.showOnePickerView(_mActivity, type, mRequest.getService_is_pawn(), list,
                        pickerEntity1 -> {
                            mRequest.setService_is_pawn(pickerEntity1.getSign_id());
                            mViewBinding.tvServiceIsPawn.setText(pickerEntity1.getTitle());
                        });
                break;
        }
    }

    /**
     * 返回费用明细信息
     *
     * @param entity
     */
    @Override
    public void returnCostDetailData(FTaskCostDetailEntity entity) {
        isNetSuccess = true;
        mCostDetailEntity = entity;
        mCostDetailEntity.setId(id);
        String[] excludsArray = new String[]{"choose_list", "id", "mates"};
        try {
            Properties.copyPropertiesExclude(entity, mRequest, excludsArray);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mViewBinding.etChannelFee.setText(entity.getChannel_fee());
        mViewBinding.etPayMoney.setText(entity.getPay_money());
        mViewBinding.etPayDate.setText(entity.getPay_date());
        mViewBinding.etServiceMoney.setText(entity.getService_money());
        FTaskCostDetailEntity.ChooseListBean choose_list = entity.getChoose_list();
        mViewBinding.tvServicePayCondition.setText(choose_list.getService_pay_condition());
        mViewBinding.tvServicePayType.setText(choose_list.getService_pay_type());
        mViewBinding.tvServiceIsPawn.setText(choose_list.getService_is_pawn());
        if ("2".equals(mRequest.getService_pay_type())) {//一次性
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.GONE);
            mRequest.setPay_money("");
            mCostDetailEntity.setPay_money("");
        } else if ("3".equals(mRequest.getService_pay_type())) {//按日
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
        } else if ("4".equals(mRequest.getService_pay_type())) {//分期
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
        } else {
            mViewBinding.llyPayDate.setVisibility(View.GONE);
            mViewBinding.llyPayMoney.setVisibility(View.GONE);
            mRequest.setPay_money("");
            mCostDetailEntity.setPay_money("");
            mRequest.setPay_date("");
            mCostDetailEntity.setPay_date("");
        }
    }

    public class ClickHandler {
        public void onShowServicePayConditionPickerView(View view) {
            pickerType = 1;
            mPresenter.loadPickerViewData("service_pay_condition", "付款条件");
        }

        public void onShowServicePayTypePickerView(View view) {
            pickerType = 2;
            mPresenter.loadPickerViewData("service_pay_type", "渠道费代收");
        }

        public void onShowServiceIsPawnPickerView(View view) {
            pickerType = 3;
            mPresenter.loadPickerViewData("service_is_pawn", "网银/卡");
        }

        public void onSave(View view) {
            saveData();
        }
    }

    /**
     * 开始保存数据请求
     */
    public void saveData() {
        setSaveData();
        if (mRequest.toString().equals(mCostDetailEntity.toString())) {
            ToastUtils.showLong("您没有更改任何信息，无需保存");
            return;
        }
        isNetSuccess = false;
        mPresenter.saveCostDetail(mRequest);
    }

    /**
     * 判断是否保存过数据 没有的话点击保存去保存
     *
     * @return 是否保存当前页面数据
     */
    public boolean isSaveData() {
        //如果请求错误则返回true
        if (!isNetSuccess) {
            return true;
        }
        setSaveData();
        if (mRequest.toString().equals(mCostDetailEntity.toString())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 设置请求数据
     */
    private void setSaveData() {
        mRequest.setService_money(mViewBinding.etServiceMoney.getText().toString().trim());
        mRequest.setChannel_fee(mViewBinding.etChannelFee.getText().toString().trim());
        if ("2".equals(mRequest.getService_pay_type())) {//一次性
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.GONE);
            mRequest.setPay_date(mViewBinding.etPayDate.getText().toString());
            mRequest.setPay_money("");
        } else if ("3".equals(mRequest.getService_pay_type())) {//按日
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
            mRequest.setPay_date(mViewBinding.etPayDate.getText().toString());
            mRequest.setPay_money(mViewBinding.etPayMoney.getText().toString());
        } else if ("4".equals(mRequest.getService_pay_type())) {//分期
            mViewBinding.llyPayDate.setVisibility(View.VISIBLE);
            mViewBinding.llyPayMoney.setVisibility(View.VISIBLE);
            mRequest.setPay_date(mViewBinding.etPayDate.getText().toString());
            mRequest.setPay_money(mViewBinding.etPayMoney.getText().toString());
        } else {
            mViewBinding.llyPayDate.setVisibility(View.GONE);
            mViewBinding.llyPayMoney.setVisibility(View.GONE);
            mRequest.setPay_date("");
            mRequest.setPay_money("");
        }
    }
}
